<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="销售单号" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入销售单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="销售日期">
        <el-date-picker v-model="daterangeOrderDate" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>


      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option v-for="dict in dict.type.order_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="前肢状态" prop="cueMaterialStatus">
        <el-select v-model="queryParams.cueMaterialStatus" placeholder="请选择前肢状态" clearable>
          <el-option v-for="dict in dict.type.cue_material_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item label="姓名" prop="cmsName">
        <el-input v-model="queryParams.cmsName" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="地址" prop="cmsAddress">
        <el-input v-model="queryParams.cmsAddress" placeholder="请输入地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="销售人员" prop="salesperson">
        <el-input v-model="queryParams.salesperson" placeholder="请输入销售人员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>



      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['order:orderInfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['order:orderInfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['order:orderInfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['order:orderInfo:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <div style="font-size : 20px">当前订单总金额{{ orderTotalAmount }}元，已支付{{ orderPaidAmount }}元，待支付{{ orderWaitAmount }}元</div>

    <el-table v-loading="loading" :data="orderInfoList" @selection-change="handleSelectionChange"
      :default-sort="defaultSort" @sort-change="handleSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="销售单号" align="center" prop="orderId" />
      <el-table-column label="销售日期" align="center" prop="orderDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.orderStatus" />
        </template>
      </el-table-column>

      <el-table-column label="前肢状态" align="center" prop="cueMaterialStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.cue_material_status" :value="scope.row.cueMaterialStatus" />
        </template>
      </el-table-column>

      <el-table-column label="客户信息" align="center" width="350">
        <template #default="scope">
          <div>姓名: {{ scope.row.cmsName }}</div>
          <div>联系电话: {{ scope.row.cmsPhone }}</div>
          <div>地址: {{ scope.row.cmsAddress }}</div>
        </template>
      </el-table-column>

      <!-- <el-table-column label="姓名" align="center" prop="cmsName" />
      <el-table-column label="联系电话" align="center" prop="cmsPhone" />
      <el-table-column label="地址" align="center" prop="cmsAddress" /> -->

      <el-table-column label="参数" align="center" width="250">
        <template #default="scope">
          <div>铭牌: {{ scope.row.cueLevel }}</div>

          <div>皮头: {{ scope.row.pitou }}</div>
          <div>重量: {{ scope.row.cueWeight }}</div>
          <div>长度: {{ scope.row.cueLength }}</div>
          <div>握把: {{ scope.row.cueGrip }}</div>
          <div>重心: {{ scope.row.cueBarycenter }}</div>


        </template>
      </el-table-column>


      <!-- <el-table-column label="皮头参数" align="center" prop="pitou" />
      <el-table-column label="重量" align="center" prop="cueWeight" />
      <el-table-column label="长度" align="center" prop="cueLength" />
      <el-table-column label="握把" align="center" prop="cueGrip" />
      <el-table-column label="重心" align="center" prop="cueBarycenter" /> -->
      <el-table-column label="销售人员" align="center" prop="salesperson" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="已付金额" align="center" prop="paidAmount" />
      <el-table-column label="验收时间" align="center" prop="acceptanceTime" width="180" :show-overflow-tooltip="true"
        sortable="custom" :sort-orders="['descending', 'ascending']">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.acceptanceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['order:orderInfo:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleOrderExport(scope.row)"
            v-hasPermi="['order:orderInfo:export']">订单导出</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['order:orderInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改订单信息管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="销售单号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入销售单号" />
        </el-form-item> -->
        <el-form-item label="销售日期" prop="orderDate">
          <el-date-picker clearable v-model="form.orderDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择销售日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="订单状态" prop="orderStatus">
          <el-radio-group v-model="form.orderStatus">
            <el-radio v-for="dict in dict.type.order_status" :key="dict.value" :label="parseInt(dict.value)">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="铭牌等级" prop="cueLevel">
          <el-radio-group v-model="form.cueLevel">
            <el-radio v-for="dict in dict.type.cue_level" :key="dict.value" :label="dict.value">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="前肢状态" prop="cueMaterialStatus">
          <el-radio-group v-model="form.cueMaterialStatus">
            <el-radio v-for="dict in dict.type.cue_material_status" :key="dict.value" :label="parseInt(dict.value)">{{
              dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="姓名" prop="cmsName">
          <el-input v-model="form.cmsName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="cmsPhone">
          <el-input v-model="form.cmsPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址" prop="cmsAddress">
          <el-input v-model="form.cmsAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="皮头参数" prop="pitou">
          <el-input v-model="form.pitou" placeholder="请输入皮头参数" />
        </el-form-item>
        <el-form-item label="重量" prop="cueWeight">
          <el-input v-model="form.cueWeight" placeholder="请输入重量" />
        </el-form-item>
        <el-form-item label="长度" prop="cueLength">
          <el-input v-model="form.cueLength" placeholder="请输入长度" />
        </el-form-item>
        <el-form-item label="握把" prop="cueGrip">
          <el-input v-model="form.cueGrip" placeholder="请输入握把" />
        </el-form-item>
        <el-form-item label="重心" prop="cueBarycenter">
          <el-input v-model="form.cueBarycenter" placeholder="请输入重心" />
        </el-form-item>
        <el-form-item label="销售人员" prop="salesperson">
          <el-input v-model="form.salesperson" placeholder="请输入销售人员" />
        </el-form-item>
        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="请输入总金额" />
        </el-form-item>
        <el-form-item label="已付金额" prop="paidAmount">
          <el-input v-model="form.paidAmount" placeholder="请输入已付金额" />
        </el-form-item>
        <el-form-item label="验收时间" prop="acceptanceTime">
          <el-date-picker clearable v-model="form.acceptanceTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择验收时间">
          </el-date-picker>
        </el-form-item>
        <el-divider content-position="center">订单明细信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddTOrderDetails">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteTOrderDetails">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="torderDetailsList" :row-class-name="rowTOrderDetailsIndex"
          @selection-change="handleTOrderDetailsSelectionChange" ref="tOrderDetails">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50" />
          <el-table-column label="商品名称" prop="goodsName" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsName" placeholder="请输入商品名称" />
            </template>
          </el-table-column>
          <el-table-column label="规格型号" prop="specifications" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.specifications" placeholder="请输入规格型号" />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="unit" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unit" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="数量" prop="number" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.number" placeholder="请输入数量" />
            </template>
          </el-table-column>
          <el-table-column label="单价" prop="unitPrice" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.unitPrice" placeholder="请输入单价" />
            </template>
          </el-table-column>
          <el-table-column label="金额" prop="price" width="100">
            <template slot-scope="scope">
              <el-input v-model="scope.row.price" placeholder="请输入金额" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrderInfo, getOrderInfo, delOrderInfo, addOrderInfo, updateOrderInfo, queryAmount } from "@/api/order/orderInfo";

export default {
  name: "OrderInfo",
  dicts: ['order_status' ,'cue_material_status','cue_level'],

  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedTOrderDetails: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单信息管理表格数据
      orderInfoList: [],
      // 订单明细表格数据
      torderDetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间时间范围
      daterangeOrderDate: [],
      // 创建时间时间范围
      daterangeAcceptanceTime: [],
      // 默认排序
      defaultSort: { prop: 'acceptanceTime', order: 'descending' },
      // 查询参数
      queryParams: {
        isAsc: 'desc',
        orderByColumn: 'order_date',
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderDate: null,
        cmsName: null,
        cmsAddress: null,
        salesperson: null,
        orderStatus: null,

      },

      orderTotalAmount: null,
      orderPaidAmount: null,
      orderWaitAmount: null,




      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // orderId: [
        //   { required: true, message: "销售单号不能为空", trigger: "blur" }
        // ],
        orderDate: [
          { required: true, message: "销售日期不能为空", trigger: "blur" }
        ],
        cmsName: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        cmsPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        cmsAddress: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        pitou: [
          { required: true, message: "皮头参数不能为空", trigger: "blur" }
        ],
        cueLevel: [
          { required: true, message: "铭牌等级不能为空", trigger: "blur" }
        ],
        cueWeight: [
          { required: true, message: "重量不能为空", trigger: "blur" }
        ],
        cueLength: [
          { required: true, message: "长度不能为空", trigger: "blur" }
        ],
        cueGrip: [
          { required: true, message: "握把不能为空", trigger: "blur" }
        ],
        cueBarycenter: [
          { required: true, message: "重心不能为空", trigger: "blur" }
        ],
        salesperson: [
          { required: true, message: "销售人员不能为空", trigger: "blur" }
        ],
        totalAmount: [
          { required: true, message: "总金额不能为空", trigger: "blur" }
        ],
        paidAmount: [
          { required: true, message: "已付金额不能为空", trigger: "blur" }
        ],
        acceptanceTime: [
          { required: true, message: "验收时间不能为空", trigger: "blur" }
        ],
        cueMaterialStatus: [
          { required: true, message: "前肢状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAmount();

  },
  methods: {
    /** 查询订单信息管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeOrderDate && '' != this.daterangeOrderDate) {
        this.queryParams.params["beginOrderDate"] = this.daterangeOrderDate[0];
        this.queryParams.params["endOrderDate"] = this.daterangeOrderDate[1];
      }
      if (null != this.daterangeAcceptanceTime && '' != this.daterangeAcceptanceTime) {
        this.queryParams.params["beginAcceptanceTime"] = this.daterangeAcceptanceTime[0];
        this.queryParams.params["endAcceptanceTime"] = this.daterangeAcceptanceTime[1];
      }
      listOrderInfo(this.queryParams).then(response => {
        this.orderInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询订单信息管理列表 */
    getAmount() {

      queryAmount().then(response => {
        this.orderTotalAmount = response.data.orderTotalAmount;
        this.orderPaidAmount = response.data.orderPaidAmount;
        this.orderWaitAmount = response.data.orderWaitAmount;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderDate: null,
        cmsName: null,
        cmsPhone: null,
        cmsAddress: null,
        pitou: null,
        cueWeight: null,
        cueLength: null,
        cueGrip: null,
        cueBarycenter: null,
        salesperson: null,
        totalAmount: null,
        paidAmount: null,
        acceptanceTime: null,
        updateTime: null,
        createTime: null
      };
      this.torderDetailsList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeOrderDate = [];
      this.daterangeAcceptanceTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderInfo(id).then(response => {
        this.form = response.data;
        this.torderDetailsList = response.data.torderDetailsList;
        this.open = true;
        this.title = "修改订单信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.torderDetailsList = this.torderDetailsList;
          if (this.form.id != null) {
            updateOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrderInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息管理编号为"' + ids + '"的数据项？').then(function () {
        return delOrderInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 订单明细序号 */
    rowTOrderDetailsIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 订单明细添加按钮操作 */
    handleAddTOrderDetails() {
      let obj = {};
      obj.goodsName = "";
      obj.specifications = "";
      obj.unit = "";
      obj.number = "";
      obj.unitPrice = "";
      obj.price = "";
      obj.remark = "";
      this.torderDetailsList.push(obj);
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 订单明细删除按钮操作 */
    handleDeleteTOrderDetails() {
      if (this.checkedTOrderDetails.length == 0) {
        this.$modal.msgError("请先选择要删除的订单明细数据");
      } else {
        const torderDetailsList = this.torderDetailsList;
        const checkedTOrderDetails = this.checkedTOrderDetails;
        this.torderDetailsList = torderDetailsList.filter(function (item) {
          return checkedTOrderDetails.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleTOrderDetailsSelectionChange(selection) {
      this.checkedTOrderDetails = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('order/orderInfo/export', {
        ...this.queryParams
      }, `orderInfo_${new Date().getTime()}.xlsx`)
    },
    /** 导出按钮操作 */
    handleOrderExport(row) {
      this.download('order/orderInfo/orderExport/' + row.id, {

      }, row.cmsName + `_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
